class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        profit=0
        buyed=False
        prices.append(0)
        buyprice=0
        for i in range(len(prices)-1):
            if buyed and prices[i]>prices[i+1]:
                buyed=False
                profit+=prices[i]-buyprice
            if not buyed and prices[i]<prices[i+1]:
                buyed=True
                buyprice=prices[i]
        return profit